package com.shcowin.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shcowin.biz.ProductsBiz;
import com.shcowin.entity.Products;

@Controller
@RequestMapping("/products")
public class ProductsController {

	@Inject
	private ProductsBiz<Products> productsBiz;

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> add(Products products) {
		productsBiz.add(products);
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("result", true);
		return json;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> edit(Products products) {
		products.setUploadDate(new Date());
		productsBiz.edit(products);
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("result", true);
		return json;
	}

	@RequestMapping(value = "/homePro")
	@ResponseBody
	public Map<String, Object> homePro(int id, int homeMark) {
		Map<String, Object> json = new HashMap<String, Object>();
		if(homeMark ==1){
			if(productsBiz.getHomeMarkCount() >=5){
				json.put("result", false);
				return json;
			}
		}
		Products products = productsBiz.getById(id);
		if (null != products) {
			products.setHomeMark(homeMark);
		}
		productsBiz.edit(products);
		json.put("result", true);
		return json;
	}

	@RequestMapping(value = "/get", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getById(Integer id) {
		Products products = productsBiz.getById(id);
		if (null == products) {
			return null;
		}
		Map<String, Object> json = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
		json.put("products", products);
		json.put("date", sdf.format(products.getUploadDate()));
		return json;
	}

	@RequestMapping("/del")
	@ResponseBody
	public Map<String, Object> del(int id) {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			productsBiz.del(id);
		} catch (Exception e) {
			json.put("result", false);
			return json;
		}
		json.put("result", true);
		return json;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> list() {
		List<Products> productsList = productsBiz.getAll();
		Map<String, Object> json = new HashMap<String, Object>();
		List<String> datas = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
		if (null != productsList && !productsList.isEmpty()) {
			json.put("productsList", productsList);
			for (Products products : productsList) {
				datas.add(sdf.format(products.getUploadDate()));
			}
			json.put("datas", datas);
		}
		return json;
	}

	@RequestMapping(value = "/getByPage", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getByPage(int currPage) {
		int start = (currPage - 1) * 10;
		List<Products> productsList = productsBiz.getByPage(start, 10);
		Map<String, Object> json = new HashMap<String, Object>();
		List<String> datas = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
		if (null != productsList && !productsList.isEmpty()) {
			json.put("productsList", productsList);
			for (Products products : productsList) {
				datas.add(sdf.format(products.getUploadDate()));
			}
			json.put("datas", datas);
			json.put("totalSize", Math.ceil(productsBiz.getCount() / 10.0));
		}
		return json;
	}

	@RequestMapping(value = "/getByHomeMark", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getByHomeMark() {
		List<Products> productsList = productsBiz.getByHomeMark();
		Map<String, Object> json = new HashMap<String, Object>();
		List<String> datas = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
		if (null != productsList && !productsList.isEmpty()) {
			json.put("productsList", productsList);
			for (Products products : productsList) {
				datas.add(sdf.format(products.getUploadDate()));
			}
			json.put("datas", datas);
			 
			json.put("totalSize", Math.ceil(productsBiz.getCount() / 10.0));
		}
		return json;
	}
}
